/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.unittests.tests;

import j2meunit.framework.Test;
import j2meunit.framework.TestCase;
import j2meunit.framework.TestMethod;
import j2meunit.framework.TestSuite;

import com.aroundj2me.utility.HttpCommunicator;

public class HttpCommunicatorTest extends TestCase {
	private String urlTest = "http://www.google.com/intl/en_ALL/images/logo.gif";
	
	
	public HttpCommunicatorTest(String name, TestMethod method) {
		super(name, method);
	}

	public HttpCommunicatorTest() {
	}
	
	protected void setUp() throws Exception {
		
	}

	protected void tearDown() throws Exception {
		
	}
	
	public void testExecuteHTTPGet(){
		byte[] result = null;
		try {
			result = HttpCommunicator.executeHTTPGet(urlTest);
		} catch (Exception e) {
			fail(e.getMessage());
			e.printStackTrace();
		}
		
		assertNotNull(result);
		assertTrue("the byte array should contain some data", result.length > 0);
	}
	
	public void testUrlEncode(){
		//test the removing of white-space characters
		String test = "Hello J2ME";
		String result = "Hello%20J2ME";
		
		test = HttpCommunicator.urlEncode(test);
		assertNotNull(test);
		
		assertEquals("the whitespace should have been encoded", result, test);		
	}
	
	public Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTest(new HttpCommunicatorTest("HttpCommunicatorTest:testExecuteHTTPGet", new TestMethod() {
			public void run(TestCase tc) {
				((HttpCommunicatorTest) tc).testExecuteHTTPGet();
			}
		}));
		suite.addTest(new HttpCommunicatorTest("HttpCommunicatorTest:testUrlEncode", new TestMethod() {
			public void run(TestCase tc) {
				((HttpCommunicatorTest) tc).testUrlEncode();
			}
		}));
		return suite;
	}
}
